package sina.girl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

import com.th.spider.Config;
import com.th.spider.ThreadPool;

public class TestGetPic {
    private static final Log log = LogFactory.getLog(TestGetPic.class);
    
    public static void main(String[] args) {
        String isProxy = Config.getSinaProps("isproxy");
        String proxyUrl = Config.getSinaProps("proxy.url");
        String proxyPort = Config.getSinaProps("proxy.port");
        
        String urlTemplate = Config.getSinaProps("sinaurl.template");
        String start = Config.getSinaProps("sina.page.start");
        String end = Config.getSinaProps("sina.page.end");
        
        HttpClient client = new DefaultHttpClient();
        if ("0".equals(isProxy)) {
            HttpHost proxy = new HttpHost(proxyUrl, Integer.parseInt(proxyPort));
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }
        
        List<String> urlList = new ArrayList<String>(0);
        
        for (int i = Integer.parseInt(start); i <= Integer.parseInt(end); i++) {
            String url = urlTemplate.replace("#page#", String.valueOf(i));
            urlList.add(url);
        }
        
        GetPic getPic = new GetPic(client);
        
        try {
            final List<List<Map<String, String>>> list = getPic.getPicInfos(urlList);
            
            ThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    SavePic saver = new SavePic();
                    try {
                        saver.save(list);
                    } catch (Exception e) {
                        log.error("保存文件失败![src=" + e.getMessage());
                    }
                }
            });
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}